<?php

/**

 * 商品分类

 *

 *

 * * @网店运维 (c) 2015-2018  Inc. (http://www.shopwwi.com)
 * @license    http://www.shopwwi.c om
 * @link       交流群号：111731672
 * @since      网店运维提供技术支持 授权请购买shopnc授权

 */



//use Shopnc\Tpl;



defined('InShopNC') or exit('Access Invalid!');

class goods_classControl extends mobileHomeControl{



    public function __construct() {
        parent::__construct();
    }
    public function indexOp() {
      /*  if(!empty($_GET['gc_id']) && intval($_GET['gc_id']) > 0) {
            $this->_get_class_list($_GET['gc_id']);
        } else {
            $this->_get_root_class();
        }*/
        $model_brand = Model("brand");
        $condition = array('brand_apply'=>'1');
        $order = 'brand_sort desc, brand_id desc';
        if (in_array($_GET['key'],array('1','2'))) {
            $sequence = $_GET['order'] == '1' ? 'asc' : 'desc';
            $order = str_replace(array('1','2'), array('brand_sort','brand_recommend'), $_GET['key']);
            $order .= ' '.$sequence;
        }
        $limit = 24;
        $brands = $model_brand->getBrandList($condition, '*', $limit , $order );
        Tpl::output('brands',$brands);
        Tpl::showpage('goods_class');
    }
	public function class_listOp() {
        Tpl::showpage('goods_class_list');
    }
    public function get_child_allOp($gc_id){
		$model_class = Model('goods_class');
        $parent_goods_class = $model_class->getTreeClassList(3);//商品分类父类列表，只取到第二级
        $parent_class = array();
        if (is_array($parent_goods_class) && !empty($parent_goods_class)){
            foreach ($parent_goods_class as $k => $v){
                $gc_id = $v['gc_id'];
                $parent_class[$gc_id]['parent_name'] = $v['gc_name'];
                $parent_id = $v['gc_parent_id'];
                if ($parent_id > 0) {
                    $parent_class[$gc_id]['parent_name'] = $parent_class[$parent_id]['parent_name'].' > '.$v['gc_name'];
                }
                $parent_goods_class[$k]['gc_name'] = str_repeat("&nbsp;",$v['deep']*2).$v['gc_name'];
            }
        }
           
            output_data(array('parent_goods_class' => $parent_goods_class));
        }
    
        

    

    /**

     * 返回一级分类列表

     */

    private function _get_root_class() {
        $model_goods_class = Model('goods_class');
        $model_mb_category = Model('mb_category');
        $goods_class_array = Model('goods_class')->getGoodsClassForCacheModel();

        $class_list = $model_goods_class->getGoodsClassListByParentId(0);
        $mb_categroy = $model_mb_category->getLinkList(array());
        $mb_categroy = array_under_reset($mb_categroy, 'gc_id');
        foreach ($class_list as $key => $value) {
            if(!empty($mb_categroy[$value['gc_id']])) {
               $class_list[$key]['image'] = UPLOAD_SITE_URL.DS.ATTACH_MOBILE.DS.'category'.DS.$mb_categroy[$value['gc_id']]['gc_thumb'];
            } else {
                $class_list[$key]['image'] = '';
            }

            $class_list[$key]['text'] = '';
            $child_class_string = $goods_class_array[$value['gc_id']]['child'];
            $child_class_array = explode(',', $child_class_string);
            foreach ($child_class_array as $child_class) {
                $class_list[$key]['text'] .= $goods_class_array[$child_class]['gc_name'] . '/';
            }
            $class_list[$key]['text'] = rtrim($class_list[$key]['text'], '/');
        }

        output_data(array('class_list' => $class_list));
    }

    /**

     * 根据分类编号返回下级分类列表

     */

    private function _get_class_list($gc_id) {
        $goods_class_array = Model('goods_class')->getGoodsClassForCacheModel();
        $goods_class = $goods_class_array[$gc_id];
        if(empty($goods_class['child'])) {
            //无下级分类返回0
            output_data(array('class_list' => '0'));
        } else {
            //返回下级分类列表
            $class_list = array();
            $child_class_string = $goods_class_array[$gc_id]['child'];
            $child_class_array = explode(',', $child_class_string);
            foreach ($child_class_array as $child_class) {
                $class_item = array();
                $class_item['gc_id'] .= $goods_class_array[$child_class]['gc_id'];
                $class_item['gc_name'] .= $goods_class_array[$child_class]['gc_name'];
                $class_list[] = $class_item;
            }
            output_data(array('class_list' => $class_list));
        }
    }
	
}

